package com.live.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: RuoYi-Cloud-master
 * @ClassName LiveUtils
 * @description:
 * @author:
 * @create: 2023-11-21 14:44
 * @Version 1.0
 **/

public class LiveUtils {
    /**
     * 推流地址
     * 地址组成     推流域名  + AppName + StreamName+ 鉴权信息
     * 鉴权信息
     * RTMP 地址
     * rtmp://160242.livepush.myqcloud.com/AppName/StreamName?txSecret=md5(key+StreamName+hex(time))&txTime=hex(time)
     * WebRTC 地址
     * webrtc://160242.livepush.myqcloud.com/AppName/StreamName?txSecret=md5(key+StreamName+hex(time))&txTime=hex(time)
     * SRT 地址
     * srt://160242.livepush.myqcloud.com:9000?streamid=#!::h=160242.livepush.myqcloud.com,r=AppName/StreamName,txSecret=md5(key+StreamName+hex(time)),txTime=hex(time)
     * RTMP over SRT 地址
     * rtmp://160242.livepush.myqcloud.com:3570/AppName/StreamName?txSecret=md5(key+StreamName+hex(time))&txTime=hex(time)
     *
     * 拉流地址
     * webrtc://www.prositech.cn/AppName/StreamName
     *
     * http://www.prositech.cn/AppName/StreamName.m3u8
     * http://www.prositech.cn/2012A_Group1/game_111.m3u8
     */
    //推送域名
    public static String PUSH_DOMAIN="160242.livepush.myqcloud.com";
    //拉流域名需备案
    public static String PULL_DOMAIN="www.prositech.cn";

    public static String KEY="b2eccccef6ec6166f694c59f092bc419";

    public static String getWebRTC_PushURL(String appName, String streamName, Date endTime){
        return "webrtc://"+PULL_DOMAIN+"/"+appName+"/"+streamName+"?"+getSafeUrl(KEY,streamName,endTime.getTime()/1000);
    }

    public static String getWebRTC_PullURL(String appName,String streamName){
        return "webrtc://"+PULL_DOMAIN+"/"+appName+"/"+streamName;
    }

    public static String getM3U8_PullURL(String appName,String streamName){
        return "http://"+PULL_DOMAIN+"/"+appName+"/"+streamName+".m3u8";
    }

    public static void main(String[] args) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,1);
        Date time = calendar.getTime();
        System.out.println(date);
        System.out.println(time);

        String pushURL = getWebRTC_PushURL("yjs2103A_Group1", "game_111", time);
        String pullURL = getM3U8_PullURL("yjs2103A_Group1", "game_111");
        System.out.println(pushURL);
        System.out.println(pullURL);
    }

    private static final char[] DIGITS_LOWER=
            {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

    private static String getSafeUrl(String key, String streamName, long l) {
        String input = new StringBuffer().append(key)
                .append(streamName)
                .append(Long.toHexString(l).toUpperCase()).toString();
        String txSecret=null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            txSecret=byteArrayToHexString(messageDigest.digest(input.getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }

        return txSecret == null ? "":
                new StringBuffer()
                        .append("txSecret=")
                        .append(txSecret)
                        .append("&")
                        .append("txTine")
                        .append(Long.toHexString(l).toUpperCase())
                        .toString();

    }

    private static String byteArrayToHexString(byte[] digest) {
        char[] out = new char[digest.length << 1];

        for (int i = 0 , j = 0; i < digest.length ; i++) {
            out[j++] = DIGITS_LOWER[0xF0 & digest[i] >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & digest[i]];
        }
        return new String(out);
    }
}
